
#include <string>
#include <iostream>
#include <fstream>
#include <cstdint>
#include <map>
#include <utility>

#ifndef DIR_CFG
DIR_CFG "."
#endif

#ifndef DIR_OUTPUT
DIR_OUTPUT "."
#endif



#define QUE "que.dat"
#define QUEM "quem.dat"
#define ONDE "onde.dat"
#define DB "classificacoes.dat"

std::string get_name_pgm(const char * p_full_pgm_name){
    std::string l_rc (p_full_pgm_name);
    return l_rc;
}

std::map<std::string,std::string> load(const std::string & p_dat) {

    typedef std::map<std::string,std::string> codes;
    

    std::map<std::string,std::string> l_rc;
    
    std::ifstream l_dat(p_dat);
    std::string l_line;

    std::string l_code;
    std::string l_name;

    std::string::size_type l_pos;
    

    while(true){
        std::getline(l_dat,l_line);
        if (l_dat.eof()) {
            break;
        }

        l_pos = l_line.find(' ');

        l_code = std::move(std::string(&l_line[0], &l_line[l_pos-1]));

        l_name = std::move(std::string(&l_line[++l_pos],
                                       &l_line[l_line.size()]));



//        std::cout << l_name << ", " << l_code << std::endl;

        codes::value_type l_value(l_name, l_code);
        

        l_rc.insert(std::move(l_value));
        
        
    }

    for (codes::const_iterator l_ite = l_rc.begin();l_ite != l_rc.end();++l_ite){
        std::cout << "name = " << l_ite->first << ", code = " << l_ite->second <<
        std::endl;
    }
    
    return l_rc;
}

    



std::string find_full_code(const std::string & p_dat, const std::string & p_full_class){
    std::string l_rc;

    std::string l_path_dat(DIR_CFG + std::string("/") + p_dat);

    std::map<std::string,std::string> l_map = load(l_path_dat);

    std::string::size_type l_pos = p_full_class.find_last_of('.');

    std::string l_last_class =  (l_pos ?
                                 std::move(std::string(&p_full_class[++l_pos],
                                                       &p_full_class[p_full_class.size()]))
                                 :
                                 std::move(p_full_class) );

    std::cout << p_full_class << " last class = " << l_last_class << std::endl;

    return l_rc;
}



int main (int argc, char **argv){

    std::string l_pgm_name=std::move(get_name_pgm(argv[0]));
    

    if (argc != 5){
        std::cout << "Sintaxe: " << l_pgm_name << " <foto> <que> <quem> <onde>\n"
                  << "     ou: " << l_pgm_name << " <dir> <que> <quem> <onde>\n"
                  << "     ou: " << l_pgm_name << " <padrao> <que> <quem> <onde>\n";
        exit(1);
    }

    std::string l_alvo(argv[1]);
    std::string l_que(argv[2]);
    std::string l_quem(argv[3]);
    std::string l_onde(argv[4]);

    std::string cod_que=std::move(find_full_code(QUE, l_que));
// std::string cod_quem=std::move(find_full_code(QUEM, l_quem));
// std::string cod_onde=std::move(find_full_code(ONDE, l_onde));

    
    

}


        
